<template>
	<el-dialog title="三员事件审批" v-model="visible" customClass="customWidth" destroy-on-close
			   @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<scTable ref="table" :data="messageList" row-key="key" stripe>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="事件序号" prop="eventSeq" width="100"></el-table-column>
				<el-table-column label="属性" prop="key" width="150"></el-table-column>
				<el-table-column label="属性名称" prop="columnName" width="150"></el-table-column>
				<el-table-column label="原始值" prop="resValue" width="300"></el-table-column>
				<el-table-column label="目标值" prop="targetValue" width="300"></el-table-column>
			</scTable>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="模块名称">
						{{ moduleTitle }}
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="操作时间">
						{{ createTime }}
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="操作类型">
						<el-tag v-if="optType=='insert'" type="success" effect="dark">创建</el-tag>
						<el-tag v-else-if="optType=='update'" type="warning" effect="dark">修改</el-tag>
						<el-tag v-else-if="optType=='delete'" type="danger" effect="dark">删除</el-tag>
						<el-tag v-else-if="optType=='query'" type="danger" effect="dark">查询</el-tag>
						<el-tag v-else type="info" effect="dark">未知</el-tag>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="操作人">
						{{ userName }}
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="审批意见">
				<el-input v-model="form.ideaText" placeholder="审批意见" maxlength="380" type="textarea" rows="5" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否同意">
				<el-radio-group v-model="form.status">
					<el-radio-button label="1">同意</el-radio-button>
					<el-radio-button label="2">不同意</el-radio-button>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">审批</el-button>
		</template>
	</el-dialog>

</template>
<script>
import tool from "@/utils/tool";

export default {
	emits: ['success', 'closed'],
	components: {},
	data() {
		return {
			visible: false,
			isSaveing: false,
			userName: '',
			createTime: '',
			optType: '',
			moduleTitle: '',
			messageList: [],
			tableComment: [],
			form: {
				recordId: '',
				ideaText:'',
				status: '1',
			},
			rules: {}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open() {
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				this.isSaveing = true;
				if (valid) {
					var res = await this.$API.system.sysSecurity.setSysSecurityApproval.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async setData(data) {
			this.userName = data.userName;
			this.optType = data.optType;
			this.createTime = data.createTime;
			this.moduleTitle = data.moduleTitle;
			this.form.recordId = data.recordId;
			let tempMessageList = [];
			var res = await this.$API.system.sysSecurity.getSysSecurityEventColumnComment.get({recordId: data.recordId});
			if (res.code == 200) {
				this.tableComment = res.data;
				let resValue = null;
				if (data.resValue != undefined && data.resValue != null) {
					resValue = JSON.parse(data.resValue);
				}
				let targetValue = null;
				if (data.targetValue != undefined && data.targetValue != null) {
					targetValue = JSON.parse(data.targetValue);
				}
				let value = {};
				if (resValue == null) {
					value = targetValue;
				} else {
					value = resValue;
				}
				Object.keys(value).forEach((key) => {
					let r = "";
					if (resValue != null) {
						r = (resValue[key] == undefined || resValue[key] == null) ? '' : resValue[key];
					}
					let t = "";
					if (targetValue != null) {
						t = (targetValue[key] == undefined || targetValue[key] == null) ? '' : targetValue[key];
					}
					if (r !== t) {
						let lineKey = tool.toLine(key);
						let json = {};
						json.eventSeq = data.eventSeq;
						json.key = key;
						json.resValue = r;
						if(this.optType=='update') {
							json.targetValue = targetValue[key];
						}else{
							json.targetValue = t;
						}
						this.tableComment.forEach(item => {
							if (item.columnName == lineKey) {
								json.columnName = item.remark;
							}
						})
						if(this.optType=='update')
						{
							if( targetValue[key]!=undefined){
								tempMessageList.push(json);
							}
						}else{
							tempMessageList.push(json);
						}
					}
				})
				this.messageList = tempMessageList;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
